package com.example.bishe.controller.bj;

import com.alibaba.fastjson.JSON;
import com.example.bishe.entity.Bzks_bj;
import com.example.bishe.entity.Bzks_zy;
import com.example.bishe.entity.Jxbm;
import com.example.bishe.service.Bzks_bjService;
import com.example.bishe.service.Bzks_zyService;
import com.example.bishe.service.JxbmService;
import com.example.bishe.service.impl.Bzks_bjServiceImpl;
import com.example.bishe.service.impl.Bzks_zyServiceImpl;
import com.example.bishe.service.impl.JxbmServiceImpl;
import com.example.bishe.toolclass.CUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/4 16:24
 */
@RestController
@RequestMapping("/bj")
public class BjController {
    @Resource
    CUD CUD;
    @Autowired
    Bzks_bjService bj = new Bzks_bjServiceImpl();
    @Autowired
    Bzks_zyService zyService = new Bzks_zyServiceImpl();
    @Autowired
    JxbmService jxbmService = new JxbmServiceImpl();
    @PostMapping("/addbj")
    public String addbj(@RequestBody Bzks_bj bzks_bj) {
        int errcode = 0;//错误代码
        String errmsg =" ";//错误描述
        int id;//返回创建成功的id
        Bzks_bj bzksBj = new Bzks_bj();bzksBj.setBh(bzks_bj.getBh());
        Bzks_bj[] bzks_bjs = bj.select_bj(bzksBj);
        HashMap value = null;
        if(bzks_bj.getBh()==null)
            errmsg+="请求中没有添加班级班号";
        if(bzks_bj.getNj()==0)
            errmsg+="请求中没有添加班级所在的年级";
        if(bzks_bj.getBjmc()==null)
            errmsg+="请求中没有添加班级名称";
        if(bzks_bj.getZydm()==null)
            errmsg+="请求中没有添加班级所属专业代码，";
        if(bzks_bj.getWx_parentid()!=0){
            errmsg+="无法直接指定父部门微信id，请在专业代码设置所属专业代码，";
        }
        if(errmsg.length()>1){
            errcode = 1501;
            errmsg = errmsg.substring(1, errmsg.length() - 1) + "。";
        }else if(bzks_bjs.length==0){
            Bzks_zy zy = new Bzks_zy();zy.setDm(bzks_bj.getZydm());
            Bzks_zy[] bzks_zies = zyService.select_zy(zy);
            if(bzks_zies.length!=0){
                zy = bzks_zies[0];
                Jxbm jxbm = new Jxbm();jxbm.setDm(zy.getDwdm());
                Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                jxbm = jxbms[0];
                value = CUD.add(bzks_bj.getBjmc(),
                        zy.getWx_id()+"",
                        bzks_bj.getWx_id()!=0?bzks_bj.getWx_id()+"":"");
                if (value.get("errcode").equals("0")) {
                    bzks_bj.setYxdm(jxbm.getLs());
                    bzks_bj.setWx_id(Integer.parseInt((String) value.get("id")));
                    bzks_bj.setWx_parentid(zy.getWx_id());
                    errcode = 0;
                    //可以插入数据库操作
                    bj.insert_bj(bzks_bj);
                    errmsg += "无错误。";
                    errmsg = errmsg.substring(1, errmsg.length());
                }else {
                    errcode = 1501;
                    errmsg+= CUD.addpan(value);
                }
            }else {
                errcode = 1501;
                errmsg += "你输入的专业信息不存在。";
                errmsg = errmsg.substring(1, errmsg.length());
            }
        }else {
            errcode = 1501;
            errmsg += "你所创建的班级代码已经存在。";
            errmsg = errmsg.substring(1, errmsg.length());
        }
        Map<String, String> map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        if (bzks_bj.getBjmc() == null) {
            map.put("mingcheng", "null");
        } else {
            map.put("mingcheng", bzks_bj.getBjmc());
        }
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/deletebj")
    public String deletebj(@RequestBody Bzks_bj bzks_bj) {
        int errcode = 0;//错误代码
        String errmsg = "";//错误描述
        Bzks_bj bzksBj = new Bzks_bj();bzksBj.setBh(bzks_bj.getBh());
        Bzks_bj[] bzks_bjs = bj.select_bj(bzksBj);
        if (bzks_bj.getBh() == null) {
            errcode = 1502;
            errmsg = "请传入你要删除的班级班号";
        }else if(bzks_bjs.length!=0){
            int wx_id = bzks_bjs[0].getWx_id();
            bzksBj = bzks_bjs[0];
            HashMap<String, String> value = CUD.delete(bzksBj.getWx_id()+"");
            if (value.get("errcode").equals("0")) {
                bj.delete_bj(bzksBj);
                errmsg = "已删除对应的班级";
            } else {
                errmsg+= CUD.deletepan(value);
                errcode=1502;
            }
        }else {
            errcode = 1502;
            errmsg = "没有找到你想要删除的班级信息，请确认输入信息是否正确。";
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/updatebj")
    public String updatebj(@RequestBody Bzks_bj bzks_bj) {
        int errcode = 0;//错误代码
        String errmsg ="";//错误描述
        if(bzks_bj.getWx_id()!=0||bzks_bj.getWx_parentid()!=0){
            errcode = 1503;
            errmsg = "不允许直接修改企业微信id及所属部门id，请按照班级所属的专业代码来进行修改。";
        }else if (bzks_bj.getBh() == null) {
            errcode = 1503;
            errmsg = "请传入你要修改的班号";
        }else {
            Bzks_bj bzksBj = new Bzks_bj();bzksBj.setBh(bzks_bj.getBh());
            Bzks_bj[] bzks_bjs = bj.select_bj(bzksBj);
            if (bzks_bjs.length!=0){
                bzksBj=bzks_bjs[0];/*对应输入的班级号码，查找到的班级信息*/
                if(bzks_bj.getZydm()!=null){
                    Bzks_zy bzksZy = new Bzks_zy();
                    bzksZy.setDm(bzks_bj.getZydm());
                    Bzks_zy[] bzks_zies = zyService.select_zy(bzksZy);/*根据专业信息，查找到的专业信息代码*/
                    if(bzks_zies.length!=0){
                        bzksZy = bzks_zies[0];
                        Jxbm jxbm = new Jxbm();jxbm.setDm(bzksZy.getDwdm());
                        Jxbm[] jxbms = jxbmService.selectJxbm(jxbm);
                        jxbm = jxbms[0];
                        HashMap<String, String> value = CUD.update(bzksBj.getWx_id()+"",
                                bzks_bj.getBjmc()!=null?bzks_bj.getBjmc():"",
                                bzksZy.getWx_id()+"");
                        if(value.get("errcode").equals("0")){
                            errmsg+="已修改对应信息";
                            bzks_bj.setZydm(bzksZy.getDm());
                            bzks_bj.setWx_parentid(bzksZy.getWx_id());
                            bzks_bj.setYxdm(jxbm.getLs());
                            bj.update_bj(bzks_bj);
                        }else {
                            errcode=1503;
                            errmsg+= CUD.updatepan(value);
                        }
                    }else {
                        errcode = 1503;
                        errmsg = "你修改的专业代码不存在";
                    }
                }else {
                    HashMap<String, String> value = CUD.update(bzksBj.getWx_id()+"",
                            bzks_bj.getBjmc()!=null?bzks_bj.getBjmc():bzksBj.getBjmc(),
                            bzksBj.getWx_parentid()+"");
                    System.out.println(value.get("errcode"));System.out.println(value.get("errmsg"));
                    if(value.get("errcode").equals("0")){
                        errmsg+="已修改对应信息";
                        bj.update_bj(bzks_bj);
                    }else {
                        errcode=1503;
                        errmsg+= CUD.updatepan(value);
                    }
                }
            }else {
                errcode = 1503;
                errmsg = "你修改的班级编号不存在";
            }
        }
        Map map = new HashMap();
        map.put("errcode", String.valueOf(errcode));
        map.put("errmsg", errmsg);
        String s = JSON.toJSONString(map);
        return s;
    }
    @PostMapping("/selectbj")
    public String selectbj(@RequestBody Bzks_bj bzks_bj) {
        Bzks_bj[] bzks_bjs = bj.select_bj(bzks_bj);
        List<Bzks_bj> list = new ArrayList<>();
        for (int i = 0; i < bzks_bjs.length; i++) {
            list.add(bzks_bjs[i]);
        }
        Map map = new HashMap();
        map.put("Bzks_bj",list);
        String s = JSON.toJSONString(map);
        return s;
    }
}
